package clientproject;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.net.Socket;

import javax.swing.JButton;
import javax.swing.JTextField;

import serverproject.model.Account;

import clientproject.window.ClientWindow;

public class SenderThread extends Thread
{
	Socket			socket;
	Account			account;
	ObjectOutputStream oos;
	ClientWindow	clientWindow;

	SenderThread(Socket socket , Account account , ClientWindow clientWindow)
	{
		this.socket = socket;
		this.account = account;
		this.clientWindow = clientWindow;

		final JButton button = clientWindow.getButton();
		final JTextField textField = clientWindow.getTextField();
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0)
			{
				try
				{
					oos.writeObject(textField.getText());

					textField.setText("");
				} catch (Exception e)
				{
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
	}

	public void run()
	{
		try
		{
			BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
			oos = new ObjectOutputStream(socket.getOutputStream());

			oos.writeObject(account);
			while (true)
			{
				String str = reader.readLine();
				oos.writeObject(str);
			}
		} catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally
		{
			try
			{
				socket.close();
			} catch (Exception ignored)
			{

			}
		}
	}
}
